<cfcomponent rest="true" restpath="/important">
	
	<!--- --->
	
	<cffunction name="getImportant" access="remote" httpmethod="GET" restpath="/getImportant">
		<cfargument name="userID" required="true" type="string" restArgSource="query" />
		<cfargument name="lastSync" required="false" default="" type="string" restArgSource="query" />
		
		<cfset var local = {} />
		
		<cfset local.user = $.request.setSession(arguments.userID) />
		
		<cfset local.response = {
			status=400,
			headers={},
			content=serializeJSON({error:"No Response"})
		} />
		
		<!--- when using modified_after, include recently deleted items --->
		<cfif arguments.lastSync neq "">
			<cfset arguments.modified_after = arguments.lastSync />
			<cfset arguments.active = "" />
		</cfif>
		
		<cftry>
			<cfif local.user.recordcount neq "1">
				<cfset local.response.status = 401 />
				<cfset local.response.content = "Authorization Failed" />
			<cfelse>
				<!--- get the events --->
				<cfset local.importants = $.run.it('importantService.getImportant',arguments) />
				
				<cfset local.importantArray = [] />
				<cfloop query="local.importants">
					<cfset local.important = {
						id=local.importants.id,
						sort_order=local.importants.sort_order,
						name=local.importants.name,
						complete=local.importants.complete,
						modified=local.importants.modified,
						active=local.importants.active
					} />
					<cfset arrayAppend(local.importantArray,local.important) />
				</cfloop>

				<cfset local.response.status = 200 />
				<cfset local.response.content = serializeJSON(local.importantArray) />
			</cfif>

			<cfset restSetResponse(local.response) />
			
			<cfcatch>
				<cfset application.helpers.log.add(
					name="REST_Error",
					method="getImportant",
					args=arguments,
					cfcatch=cfcatch
				) />
				<cfset local.response.status = "500" />
				<cfset local.response.content = "The server has encountered an error processing this request." />

				<!--- <cfsavecontent variable="local.dump">
					<cfdump var="#cfcatch#">
				</cfsavecontent>
				<cfset local.response.content = local.dump /> --->

				<cfset restSetResponse(local.response) />
			</cfcatch>
		</cftry>
		
	</cffunction>
	
	<!--- --->
	
	<cffunction name="saveImportant" access="remote" httpmethod="GET" restpath="/saveImportant">
		<cfargument name="userID" required="true" type="string" restArgSource="query" />
		<cfargument name="importantID" required="true" type="string" restArgSource="query" />
		<cfargument name="name" required="true" type="string" restArgSource="query" />
		
		<cfset var local = {} />
		
		<cfset local.user = $.request.setSession(arguments.userID) />
		
		<cfset local.response = {
			status=400,
			headers={},
			content=serializeJSON({error:"No Response"})
		} />
		
		<cftry>
			<cfif local.user.recordcount neq 1>
				<cfset local.response.status = 401 />
				<cfset local.response.content = "Authorization Failed" />
			<cfelseif arguments.name eq "">
				<cfset local.response.status = 400 />
				<cfset local.response.content = "Invalid name" />
			<cfelseif arguments.importantID neq "" and len(arguments.importantID) neq 32>
				<cfset local.response.status = 400 />
				<cfset local.response.content = "Invalid ImportantID" />
			<cfelse>
				<!--- get the expiration date --->
				<cfset local.expiration_date = $.run.it('importantService.getExpirationDate') />
				<cfif arguments.importantID eq "">
					<!--- new important --->
					<!--- get current important for sort_order --->
					<cfset local.importants = $.run.it('importantService.getImportant',arguments) />
					<cfset local.sort_order = local.importants.recordcount + 1 />
					<cfset local.response.content = $.run.it('importantService.saveImportant',{
						id="",
						table="important",
						name=arguments.name,
						sort_order=local.sort_order,
						expiration_date=local.expiration_date
					}) />
				<cfelse>
					<cfset local.response.content = $.database.save(
						table="important",
						id=arguments.importantID,
						name=arguments.name,
						expiration_date=local.expiration_date
					) />
				</cfif>
				
				<cfset local.response.status = 200 />
			</cfif>

			<cfset restSetResponse(local.response) />
			
			<cfcatch>
				<cfset application.helpers.log.add(
					name="REST_Error",
					method="saveImportant",
					args=arguments,
					cfcatch=cfcatch
				) />
				<cfset local.response.status = "500" />
				<cfset local.response.content = "The server has encountered an error processing this request." />

				<!--- <cfsavecontent variable="local.dump">
					<cfdump var="#cfcatch#">
				</cfsavecontent>
				<cfset local.response.content = local.dump /> --->

				<cfset restSetResponse(local.response) />
			</cfcatch>
		</cftry>
		
	</cffunction>
	
	<!--- --->
	
	<cffunction name="deleteImportant" access="remote" httpmethod="GET" restpath="/deleteImportant">
		<cfargument name="userID" required="true" type="string" restArgSource="query" />
		<cfargument name="importantID" required="true" type="string" restArgSource="query" />
		
		<cfset var local = {} />
		
		<cfset local.user = $.request.setSession(arguments.userID) />
		
		<cfset local.response = {
			status=400,
			headers={},
			content=serializeJSON({error:"No Response"})
		} />
		
		<cftry>
			<cfif local.user.recordcount neq 1>
				<cfset local.response.status = 401 />
				<cfset local.response.content = "Authorization Failed" />
			<cfelseif len(arguments.importantID) neq 32>
				<cfset local.response.status = 400 />
				<cfset local.response.content = "invalid importantID" />
			<cfelse>
				<cfset $.run.it('importantService.remove',{
					id=arguments.importantID
				}) />
			</cfif>
			<cfset local.response.status = 200 />
			<cfset local.response.content = "true" />
			<cfset restSetResponse(local.response) />
			
			<cfcatch>
				<cfset application.helpers.log.add(
					name="REST_Error",
					method="deleteImportant",
					args=arguments,
					cfcatch=cfcatch
				) />
				<cfset local.response.status = "500" />
				<cfset local.response.content = "The server has encountered an error processing this request." />

				<!--- <cfsavecontent variable="local.dump">
					<cfdump var="#cfcatch#">
				</cfsavecontent>
				<cfset local.response.content = local.dump /> --->

				<cfset restSetResponse(local.response) />
			</cfcatch>
		</cftry>
		
	</cffunction>
	
	<!--- --->
	
	<cffunction name="completeImportant" access="remote" httpmethod="GET" restpath="/completeImportant">
		<cfargument name="userID" required="true" type="string" restArgSource="query" />
		<cfargument name="importantID" required="true" type="string" restArgSource="query" />
		<cfargument name="complete" required="true" type="string" restArgSource="query" />
		
		<cfset var local = {} />
		
		<cfset local.user = $.request.setSession(arguments.userID) />
		
		<cfset local.response = {
			status=400,
			headers={},
			content=serializeJSON({error:"No Response"})
		} />
		
		<cftry>
			<cfif local.user.recordcount neq 1>
				<cfset local.response.status = 401 />
				<cfset local.response.content = "Authorization Failed" />
			<cfelseif len(arguments.importantID) neq 32>
				<cfset local.response.status = 400 />
				<cfset local.response.content = "invalid importantID" />
			<cfelseif arguments.complete neq 1 and arguments.complete neq 0>
				<cfset local.response.status = 400 />
				<cfset local.response.content = "invalid complete" />
			<cfelse>
				<cfset $.run.it('importantService.saveComplete',{
					id=arguments.importantID,
					complete=arguments.complete
				}) />
			</cfif>
			<cfset local.response.status = 200 />
			<cfset local.response.content = "true" />
			<cfset restSetResponse(local.response) />
			
			<cfcatch>
				<cfset application.helpers.log.add(
					name="REST_Error",
					method="completeImportant",
					args=arguments,
					cfcatch=cfcatch
				) />
				<cfset local.response.status = "500" />
				<cfset local.response.content = "The server has encountered an error processing this request." />

				<!--- <cfsavecontent variable="local.dump">
					<cfdump var="#cfcatch#">
				</cfsavecontent>
				<cfset local.response.content = local.dump /> --->

				<cfset restSetResponse(local.response) />
			</cfcatch>
		</cftry>
		
	</cffunction>
	
	<!--- --->

</cfcomponent>